package com.zhitan.ems.app.patrol.controller;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhitan.ems.app.patrol.entity.PatrolPointEntity;
import com.zhitan.ems.app.patrol.service.PatrolPointService;
import com.zhitan.ems.app.patrol.vo.PatrolPointVO;
import com.zhitan.ems.app.patrol.wrapper.PatrolPointWrapper;
import com.zhitan.ems.common.core.controller.BaseController;
import com.zhitan.ems.common.core.domain.R;
import com.zhitan.ems.common.utils.DateUtils;
import com.zhitan.ems.common.utils.StringUtils;
import com.zhitan.ems.framework.core.vo.VPage;
import com.zhitan.ems.framework.core.vo.VQueryKit;

import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
 * 巡检点
 * @author zhitan
 * 
 * @date 2024-12-03 23:16:33
 */
@Api(tags = "PC管理--巡检--巡检点", value = "巡检点")
@RestController
@RequestMapping("intra/patrol/patrolpoint")
public class PatrolPointController extends BaseController{
    @Autowired
    private PatrolPointService patrolPointService;
    @ApiOperation(value = "列表所有节点", response = VPage.class)
    @ApiImplicitParams({
		        @ApiImplicitParam(name = "name", value = "标题",  paramType = "query", required = false, dataType =  "String"),
		})
    @GetMapping("/listAll")
    @PreAuthorize("@ss.hasPermi('patrol:patrolpoint:list')")
    public R<List<PatrolPointVO>> listAll(@RequestParam Map<String, Object> params){
         QueryWrapper<PatrolPointEntity> queryWrapper=VQueryKit.getQueryWrapperByMap(params,Arrays.asList("name"), PatrolPointEntity.class);
         queryWrapper.orderByDesc("id");
         queryWrapper.like(StringUtils.isNotNull(params.get("name")),"name", params.get("name"));
         List<PatrolPointEntity> list = patrolPointService.list(queryWrapper);
        return R.data(PatrolPointWrapper.build().listVO(list));
    }
    /**
     * 列表
     */
    @ApiOperation(value = "列表", response = VPage.class)
    @ApiImplicitParams({
		        @ApiImplicitParam(name = "name", value = "标题",  paramType = "query", required = false, dataType =  "String"),
		})
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('patrol:patrolpoint:list')")
    public R<VPage<PatrolPointVO>> list(@RequestParam Map<String, Object> params){
        QueryWrapper<PatrolPointEntity> queryWrapper=VQueryKit.getQueryWrapperByMap(params,Arrays.asList("name"), PatrolPointEntity.class);
        queryWrapper.orderByDesc("id");
         queryWrapper.like(StringUtils.isNotNull(params.get("name")),"name", params.get("name"));
        IPage<PatrolPointEntity> pages = patrolPointService.page(VQueryKit.getPage(params), queryWrapper);
        return R.data(PatrolPointWrapper.build().toVPageVO(pages));
    }
    @GetMapping("/listStat")
    @PreAuthorize("@ss.hasPermi('patrol:patrolpoint:list')")
    public R<Map<String, Object>> listStat(@RequestParam Map<String, Object> params)throws ParseException{
         QueryWrapper<PatrolPointEntity> queryWrapper=VQueryKit.getQueryWrapperByMap(params,Arrays.asList("name"), PatrolPointEntity.class);
         queryWrapper.select("count(id) as total,sum(id) as all");
         queryWrapper.like(StringUtils.isNotNull(params.get("name")),"name", params.get("name"));
        if(params.containsKey("createDate")) {
            queryWrapper.between("create_at",
                    DateUtils.getStartOfDate(DateUtils.str_ToDate(params.get("createDate").toString())),
                    DateUtils.getEndOfDate(DateUtils.str_ToDate(params.get("createDate").toString()))
              );   
        }
        Map<String, Object>  stat=patrolPointService.getMap(queryWrapper);
        return R.data(stat);
    }
    /**
     * 信息
     */
    @ApiOperation(value = "信息", response = PatrolPointVO.class)
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPermi('patrol:patrolpoint:info')")
    public R<PatrolPointVO> detail(@PathVariable("id") Long id){
		PatrolPointEntity patrolPoint = patrolPointService.getById(id);
	    return R.data(PatrolPointWrapper.build().entityVO(patrolPoint));
    }
     /**
     * 保存&更新
     */
    @ApiOperation(value = "保存&更新", response = Boolean.class)
    @PostMapping("/submit")
    @PreAuthorize("@ss.hasPermi('patrol:patrolpoint:save')")
    public R<PatrolPointVO> submit(@Valid @RequestBody PatrolPointVO patrolPoint){
   		if(patrolPoint.getId()==null) {
            patrolPoint.setCreateAt(new Date());
            patrolPoint.setIsDelete(0);
            if(patrolPoint.getQrCode()==null) {
            	patrolPoint.setQrCode(IdUtil.objectId());
            }
        } 
   		patrolPoint.setUpdateAt(new Date());
    	if(patrolPointService.saveOrUpdate(patrolPoint)) {
    		 return R.ok(patrolPoint);
    	 }
    	return R.error("数据错误");
    }
    /**
     * 删除
     */
    @ApiOperation(value = "删除", response = Boolean.class)
    @DeleteMapping("/delete")
    @PreAuthorize("@ss.hasPermi('patrol:patrolpoint:delete')")
    public R<Boolean> delete(@RequestBody Long[] ids){
        return R.ok(patrolPointService.removeByIds(Arrays.asList(ids)));
    }

}
